import request from '@/utils/request'
import type { 
  FieldMapping, 
  CollisionCheckConfig, 
  DataProcessingConfig, 
  PageResponse,
  FieldMappingCreateData,
  CollisionCheckCreateData,
  ConfigCreateData
} from '@/types/api'

// 字段映射相关接口
export const getFieldMappingList = (params: any): Promise<PageResponse<FieldMapping>> => {
  return request({
    url: '/engine/field-mapping/list',
    method: 'get',
    params
  })
}

export const createFieldMapping = (data: FieldMappingCreateData): Promise<any> => {
  return request({
    url: '/engine/field-mapping',
    method: 'post',
    data
  })
}

export const updateFieldMapping = (id: number, data: FieldMappingCreateData): Promise<any> => {
  return request({
    url: `/engine/field-mapping/${id}`,
    method: 'put',
    data
  })
}

export const deleteFieldMapping = (id: number): Promise<any> => {
  return request({
    url: `/engine/field-mapping/${id}`,
    method: 'delete'
  })
}

export const updateFieldMappingStatus = (id: number, status: number): Promise<any> => {
  return request({
    url: `/engine/field-mapping/${id}/status`,
    method: 'put',
    params: { status }
  })
}

// 撞库检测相关接口
export const getCollisionCheckList = (params: any): Promise<PageResponse<CollisionCheckConfig>> => {
  return request({
    url: '/engine/collision-check/list',
    method: 'get',
    params
  })
}

export const createCollisionCheck = (data: CollisionCheckCreateData): Promise<any> => {
  return request({
    url: '/engine/collision-check',
    method: 'post',
    data
  })
}

export const updateCollisionCheck = (id: number, data: CollisionCheckCreateData): Promise<any> => {
  return request({
    url: `/engine/collision-check/${id}`,
    method: 'put',
    data
  })
}

export const deleteCollisionCheck = (id: number): Promise<any> => {
  return request({
    url: `/engine/collision-check/${id}`,
    method: 'delete'
  })
}

export const updateCollisionCheckStatus = (id: number, status: number): Promise<any> => {
  return request({
    url: `/engine/collision-check/${id}/status`,
    method: 'put',
    params: { status }
  })
}

// 数据处理配置相关接口
export const getDataProcessingConfigList = (params: any): Promise<PageResponse<DataProcessingConfig>> => {
  return request({
    url: '/engine/config/list',
    method: 'get',
    params
  })
}

export const createDataProcessingConfig = (data: ConfigCreateData): Promise<any> => {
  return request({
    url: '/engine/config',
    method: 'post',
    data
  })
}

export const updateDataProcessingConfig = (id: number, data: ConfigCreateData): Promise<any> => {
  return request({
    url: `/engine/config/${id}`,
    method: 'put',
    data
  })
}

export const deleteDataProcessingConfig = (id: number): Promise<any> => {
  return request({
    url: `/engine/config/${id}`,
    method: 'delete'
  })
}

export const updateDataProcessingConfigStatus = (id: number, status: number): Promise<any> => {
  return request({
    url: `/engine/config/${id}/status`,
    method: 'put',
    params: { status }
  })
}

// 脚本配置相关接口
export const getScriptConfigList = (params: any): Promise<PageResponse<any>> => {
  return request({
    url: '/engine/script/list',
    method: 'get',
    params
  })
}

export const createScriptConfig = (data: any): Promise<any> => {
  return request({
    url: '/engine/script',
    method: 'post',
    data
  })
}

export const updateScriptConfig = (id: number, data: any): Promise<any> => {
  return request({
    url: `/engine/script/${id}`,
    method: 'put',
    data
  })
}

export const updateScriptConfigStatus = (id: number, status: number): Promise<void> => {
  return request.put(`/engine/scripts/${id}/status`, { status })
}

export const deleteScriptConfig = (id: number): Promise<any> => {
  return request({
    url: `/engine/script/${id}`,
    method: 'delete'
  })
}

// 验证规则相关接口
export const getValidationRulesList = (params: any): Promise<PageResponse<any>> => {
  return request({
    url: '/engine/validation/list',
    method: 'get',
    params
  })
}

export const createValidationRule = (data: any): Promise<any> => {
  return request({
    url: '/engine/validation',
    method: 'post',
    data
  })
}

export const updateValidationRule = (id: number, data: any): Promise<any> => {
  return request({
    url: `/engine/validation/${id}`,
    method: 'put',
    data
  })
}

export const updateValidationRuleStatus = (id: number, status: number): Promise<void> => {
  return request.put(`/engine/validation/${id}/status`, { status })
}

export const deleteValidationRule = (id: number): Promise<any> => {
  return request({
    url: `/engine/validation/${id}`,
    method: 'delete'
  })
}

// 引擎执行相关接口
export const executeDataProcessing = (configId: number): Promise<any> => {
  return request({
    url: `/engine/execute/${configId}`,
    method: 'post'
  })
}

export const getEngineStatus = (): Promise<any> => {
  return request({
    url: '/engine/status',
    method: 'get'
  })
}

export const startEngine = (): Promise<any> => {
  return request({
    url: '/engine/start',
    method: 'post'
  })
}

export const stopEngine = (): Promise<any> => {
  return request({
    url: '/engine/stop',
    method: 'post'
  })
}